package comply.zyc.controller;

import comply.zyc.Ku;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/6/21 22:29
 * @version: v1.0
 * @modified By:
 */

@WebServlet("/xiugai")
public class XiugaiServlet extends HttpServlet {
    //实现用户修改的功能
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ////通关请求获取传过来的参数
        String nameby=(String)req.getSession().getAttribute("username");
        String passwordby=req.getParameter("password");
        PrintWriter writer = resp.getWriter();
        Connection connection= Ku.getConnection();




        try {
            //实现对应的sql  语法和防止 sql注入
            PreparedStatement stmt = connection.prepareStatement("update  loding set password =? where name=?");
            stmt.setString(1,passwordby);
            stmt.setString(2,nameby);
            int i = stmt.executeUpdate();
            //对应的逻辑判断。
            if (i==0){
                String result="修改失败";
                req.setAttribute("result",result);
                req.getRequestDispatcher("home.jsp").forward(req,resp);
            }else{

                String result="修改成功";
                req.setAttribute("result",result);
                req.getRequestDispatcher("login.jsp").forward(req,resp);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }




    }
}
